/**
 * Created by linxh on 2015/6/8.
 */
define('framework/tooltip/GillionTooltipServiceConstructor',[],function(){

    /**
     * 通过对dom节点改变的方法
     * @param element {obj} 需要创建message的dom元素
     * @param params.message {string}
     * @param params.title {string} title属性 用于做提示内容
     * @param params.gTooltip {string} gTooltip属性 用于做提示内容 （优先于title）
     * @param params.direction {string} direction属性 显示方位 默认 （right）
     * @constructor
     */
    function Messages(element, params){
        this.element = element;
        this.setParams(params);

    }

    Messages.prototype.setParams= function(params){
        if(params){
            this.title = params.title;
            this.gTooltip = params.gTooltip;
            this.direction = params.direction;
            this.fontColor = !!params.fontColor?params.fontColor:'#737373';
            this.iconType = !!params.iconType?params.iconType:'fi-help';
            this.iconColor = !!params.iconColor?params.iconColor:'#00b7ee';
            this.outterBox = !!params.outterBox?params.outterBox:false;//外框样式
            this.className = params.className;
        }
    }
    /**
     * 获取消息内容 优先取gtooltip属性的值
     * @returns {*}
     */
    Messages.prototype.getMessage = function(){
        if(this.gTooltip){
            return this.gTooltip;
        }else{
            return this.title;
        }
    };
    /**
     * 计算方向
     * @param me
     * @param next
     * @returns {*}
     */
    Messages.prototype.calculateDirection = function(me,next){
        if(next.offsetWidth+me.offsetWidth + me.offsetLeft+30<document.body.clientWidth){
            return 'right';
        }else if(next.offsetHeight<me.offsetTop){
            return 'top';
        }else if(next.offsetHeight +me.offsetHeight+me.offsetTop<document.body.clientHeight){
            return 'bottom';
        }else{
            return 'right';
        }
    };
    /**
     * 显示信息
     */
    Messages.prototype.show = function () {
        if(!!this.timeout){
            clearTimeout(this.timeout);
        }
        this.timeout = undefined;
        var message = '';
        if(this.gTooltip){
            message = this.gTooltip;
        }else{
            message = this.title;
        }
        //this.relocate();
        this.element.next().css('display','block');
        //this.createMessageDom();
    };
    /**
     * 隐藏信息
     */
    Messages.prototype.hide = function () {
        var next = this.element.next();
        this.timeout = setTimeout(function(){next.css('display','none');},300);
    };
    /**
     * 设置方位
     * @param element
     */
    Messages.prototype.locate = function(element){
        var me = element[0];
        var box = element.next();
        var next = box[0];
        this.direction = !!this.direction?this.direction:this.calculateDirection(me,next);
        switch(this.direction){
            case 'right':
                if(this.outterBox){
                    box.css("left",me.offsetLeft + me.offsetWidth+15);
                }else{
                    box.css("left",me.offsetLeft + me.offsetWidth+5);
                }
                box.addClass("right");
                box.css("top",me.offsetTop);
                break;
            case 'top':
                if(this.outterBox){
                    box.css("top",me.offsetTop-me.offsetHeight-15);
                }else{
                    box.css("top",me.offsetTop-me.offsetHeight-5);
                }
                box.addClass("tla");
                box.css("left",me.offsetLeft-(next.offsetWidth-me.offsetWidth));
                break;
            case 'left':
                break;
            case 'bottom':
                if(this.outterBox) {
                    box.css("top",me.offsetTop+me.offsetHeight+10);
                }else{
                    box.css("top",me.offsetTop+me.offsetHeight+5);
                }
                box.addClass("bra");
                box.css("left",me.offsetLeft-(next.offsetWidth-me.offsetWidth));
                break;
            default:

                ;
        }
    }
    /**
     * 创建提示框元素
     */
    Messages.prototype.createMessageDom = function(){

        this.timeout = undefined;
        var message=this.getMessage(),
            that = this;
        //var spanHtml='<div class="hint-msgs msg-s0" style="display:block">'
        //    +'<i class="msg-icon iconfont"></i><em>'+message+'</em></div>';

        var spanHtml = '<div class="popover" style="display: block;">'+
            '<div class="arrow"></div>'+
            '<div class="popover-content"><i class="fi "></i>'+message+'</div>'+
            '</div>'
        if(this.element.next("div.popover")){
            this.element.next("div.popover").remove();
        }
        this.element.after(spanHtml);
        var me = this.element[0];
        var box = this.element.next();
        var next = box[0];
        var inext = box.find("i"),
            msgNext = box.find("div.popover-content")[0];
        box.css("z-index",100);
        if(!this.outterBox){
            box.addClass("noborder");
        }

        inext.addClass(this.iconType);

        this.className?box.addClass(params.className):'';
        inext[0].style.color=this.iconColor;
        msgNext.style.color = this.fontColor;
        this.locate(this.element);
        box.on("mouseenter",function(){ that.show()});
        box.on("mouseleave",function(){ that.remove()});
    };
    /**
     * 删除提示框
     */
    Messages.prototype.remove=function(){
        var that =this;
        this.timeout = setTimeout(function(){that.element.next("div.popover").remove();},300);

    }

    return function(){
        return {
            create: function (element,params) {
                return new Messages(element,params);
            }
        };
    };
})